﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using System.Data.SqlClient;
using Model;
using System.Data;
namespace Manager
{
    /// <summary>
    /// HW_District业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_DistrictManager
    {
        			
        #region##通过主键ID得到HW_District
        /// <summary>
        /// 功能：通过ID得到HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_District GetById(string districtcode)
        {            
            using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.GetById(districtcode);
			}
        }
        public DataTable GetManager(string districtcode)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
            {
                return ws.GetManager(districtcode);
            }
        }
        #endregion
		
		#region##通过主键ID删除HW_District
        /// <summary>
        /// 功能：通过主键ID删除HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string districtcode)
		{
			using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.DeleteById(districtcode);
			}
		}
		#endregion			
		
        #region##添加HW_District
        /// <summary>
        /// 功能：添加HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_District hw_district)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.Add(hw_district);
			}
        }
        #endregion
		
		#region##得到HW_District集合
		/// <summary>
        /// 功能：得到HW_District集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_District> GetList()
		{
		    using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.GetList();
			}
		}
		#endregion
		
		#region##修改HW_District
        /// <summary>
        /// 功能：修改HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_District(HW_District hw_district)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.ModHW_District(hw_district);
			}
        }
        #endregion
		
		#region##统计HW_District
		/// <summary>
        /// 修改HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_District
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_DistrictService ws = new HW_DistrictService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_District
        /// <summary>
        /// 树形区域显示
        /// </summary>
        /// <param name="pdistrictCode"></param>
        /// <returns></returns>
        public IList<HW_District> GetSubList(string pdistrictCode)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
            {
                return ws.GetSubList(pdistrictCode);
            }
        }
        public DataTable GetDisCount(string districtCode,string yearcode)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
            {
                DataTable dt= ws.GetDisCount(districtCode,yearcode).Tables[0];
                dt.Rows.RemoveAt(0);
                int sumqty=0;
                int aqty=0;
                int bqty=0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sumqty += dt.Rows[i]["SchoolNumber"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["SchoolNumber"]);
                    aqty += dt.Rows[i]["acount"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["acount"]);
                    bqty += dt.Rows[i]["bcount"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["bcount"]);
                }
                DataRow newRow;
                newRow = dt.NewRow();
                newRow["Name"] = "总计";
                newRow["SchoolNumber"] = sumqty;
                newRow["acount"] = aqty;
                newRow["bcount"] = bqty;
                newRow["pacount"] = sumqty == 0 ? "0%" : decimal.Round((decimal)aqty*100 / (decimal)sumqty, 2).ToString() + "%";
                newRow["pbcount"] = sumqty == 0 ? "0%" : decimal.Round((decimal)bqty*100 / (decimal)sumqty, 2).ToString() + "%";
                dt.Rows.Add(newRow);

               
                return dt;
            }
        }
        public DataTable GetCityCount(string districtCode, string yearcode)
        {
            using (HW_DistrictService ws = new HW_DistrictService())
            {
                DataTable dt = ws.GetCityCount(districtCode, yearcode).Tables[0];
                int sumqty = 0;
                int aqty = 0;
                int bqty = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sumqty += dt.Rows[i]["SchoolNumber"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["SchoolNumber"]);
                    aqty += dt.Rows[i]["acount"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["acount"]);
                    bqty += dt.Rows[i]["bcount"] is DBNull ? 0 : Convert.ToInt32(dt.Rows[i]["bcount"]);
                }
                DataRow newRow;
                newRow = dt.NewRow();
                newRow["Name"] = "总计";
                newRow["SchoolNumber"] = sumqty;
                newRow["acount"] = aqty;
                newRow["bcount"] = bqty;
                newRow["pacount"] = sumqty == 0 ? "0%" : decimal.Round((decimal)aqty*100 / (decimal)sumqty, 2).ToString()+"%";
                newRow["pbcount"] = sumqty == 0 ? "0%" : decimal.Round((decimal)bqty*100 / (decimal)sumqty, 2).ToString()+"%";
                dt.Rows.Add(newRow);
                return dt;
            }
        }
        #endregion
    }
}